<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>hmac_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_crypt_hmac_test.js.html">hmac_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.crypt.HmacTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.crypt.HmacTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.crypt.Hmac&#39;);
<a name="line19"></a>goog.require(&#39;goog.crypt.Sha1&#39;);
<a name="line20"></a>goog.require(&#39;goog.crypt.hashTester&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>
<a name="line23"></a>function stringToBytes(s) {
<a name="line24"></a>  var bytes = new Array(s.length);
<a name="line25"></a>
<a name="line26"></a>  for (var i = 0; i &lt; s.length; ++i) {
<a name="line27"></a>    bytes[i] = s.charCodeAt(i) &amp; 255;
<a name="line28"></a>  }
<a name="line29"></a>  return bytes;
<a name="line30"></a>}
<a name="line31"></a>
<a name="line32"></a>function hexToBytes(str) {
<a name="line33"></a>  var arr = [];
<a name="line34"></a>
<a name="line35"></a>  for (var i = 0; i &lt; str.length; i += 2) {
<a name="line36"></a>    arr.push(parseInt(str.substring(i, i + 2), 16));
<a name="line37"></a>  }
<a name="line38"></a>
<a name="line39"></a>  return arr;
<a name="line40"></a>}
<a name="line41"></a>
<a name="line42"></a>function bytesToHex(b) {
<a name="line43"></a>  var hexchars = &#39;0123456789abcdef&#39;;
<a name="line44"></a>  var hexrep = new Array(b.length * 2);
<a name="line45"></a>
<a name="line46"></a>  for (var i = 0; i &lt; b.length; ++i) {
<a name="line47"></a>    hexrep[i * 2] = hexchars.charAt((b[i] &gt;&gt; 4) &amp; 15);
<a name="line48"></a>    hexrep[i * 2 + 1] = hexchars.charAt(b[i] &amp; 15);
<a name="line49"></a>  }
<a name="line50"></a>  return hexrep.join(&#39;&#39;);
<a name="line51"></a>}
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>/**
<a name="line55"></a> * helper to get an hmac of the given message with the given key.
<a name="line56"></a> */
<a name="line57"></a>function getHmac(key, message, opt_blockSize) {
<a name="line58"></a>  var hasher = new goog.crypt.Sha1();
<a name="line59"></a>  var hmacer = new goog.crypt.Hmac(hasher, key, opt_blockSize);
<a name="line60"></a>  return bytesToHex(hmacer.getHmac(message));
<a name="line61"></a>}
<a name="line62"></a>
<a name="line63"></a>function testBasicOperations() {
<a name="line64"></a>  var hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), &#39;key&#39;, 64);
<a name="line65"></a>  goog.crypt.hashTester.runBasicTests(hmac);
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>function testBasicOperationsWithNoBlockSize() {
<a name="line69"></a>  var hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), &#39;key&#39;);
<a name="line70"></a>  goog.crypt.hashTester.runBasicTests(hmac);
<a name="line71"></a>}
<a name="line72"></a>
<a name="line73"></a>function testHmac() {
<a name="line74"></a>  // HMAC test vectors from:
<a name="line75"></a>  // http://tools.ietf.org/html/2202
<a name="line76"></a>
<a name="line77"></a>  assertEquals(&#39;test 1 failed&#39;,
<a name="line78"></a>      &#39;b617318655057264e28bc0b6fb378c8ef146be00&#39;,
<a name="line79"></a>      getHmac(hexToBytes(&#39;0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b&#39;),
<a name="line80"></a>          stringToBytes(&#39;Hi There&#39;)));
<a name="line81"></a>
<a name="line82"></a>  assertEquals(&#39;test 2 failed&#39;,
<a name="line83"></a>      &#39;effcdf6ae5eb2fa2d27416d5f184df9c259a7c79&#39;,
<a name="line84"></a>      getHmac(stringToBytes(&#39;Jefe&#39;),
<a name="line85"></a>          stringToBytes(&#39;what do ya want for nothing?&#39;)));
<a name="line86"></a>
<a name="line87"></a>  assertEquals(&#39;test 3 failed&#39;,
<a name="line88"></a>      &#39;125d7342b9ac11cd91a39af48aa17b4f63f175d3&#39;,
<a name="line89"></a>      getHmac(hexToBytes(&#39;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#39;),
<a name="line90"></a>          hexToBytes(&#39;dddddddddddddddddddddddddddddddddddddddd&#39; +
<a name="line91"></a>              &#39;dddddddddddddddddddddddddddddddddddddddd&#39; +
<a name="line92"></a>              &#39;dddddddddddddddddddd&#39;)));
<a name="line93"></a>
<a name="line94"></a>  assertEquals(&#39;test 4 failed&#39;,
<a name="line95"></a>      &#39;4c9007f4026250c6bc8414f9bf50c86c2d7235da&#39;,
<a name="line96"></a>      getHmac(hexToBytes(&#39;0102030405060708090a0b0c0d0e0f10111213141516171819&#39;),
<a name="line97"></a>          hexToBytes(&#39;cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd&#39; +
<a name="line98"></a>              &#39;cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd&#39; +
<a name="line99"></a>              &#39;cdcdcdcdcdcdcdcdcdcd&#39;)));
<a name="line100"></a>
<a name="line101"></a>  assertEquals(&#39;test 5 failed&#39;,
<a name="line102"></a>      &#39;4c1a03424b55e07fe7f27be1d58bb9324a9a5a04&#39;,
<a name="line103"></a>      getHmac(hexToBytes(&#39;0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c&#39;),
<a name="line104"></a>          stringToBytes(&#39;Test With Truncation&#39;)));
<a name="line105"></a>
<a name="line106"></a>  assertEquals(&#39;test 6 failed&#39;,
<a name="line107"></a>      &#39;aa4ae5e15272d00e95705637ce8a3b55ed402112&#39;,
<a name="line108"></a>      getHmac(hexToBytes(&#39;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#39; +
<a name="line109"></a>              &#39;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#39; +
<a name="line110"></a>              &#39;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#39; +
<a name="line111"></a>              &#39;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#39;),
<a name="line112"></a>          stringToBytes(
<a name="line113"></a>              &#39;Test Using Larger Than Block-Size Key - Hash Key First&#39;)));
<a name="line114"></a>
<a name="line115"></a>  assertEquals(&#39;test 7 failed&#39;,
<a name="line116"></a>      &#39;b617318655057264e28bc0b6fb378c8ef146be00&#39;,
<a name="line117"></a>      getHmac(hexToBytes(&#39;0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b&#39;),
<a name="line118"></a>          stringToBytes(&#39;Hi There&#39;), 64));
<a name="line119"></a>
<a name="line120"></a>  assertEquals(&#39;test 8 failed&#39;,
<a name="line121"></a>      &#39;941f806707826395dc510add6a45ce9933db976e&#39;,
<a name="line122"></a>      getHmac(hexToBytes(&#39;0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b&#39;),
<a name="line123"></a>          stringToBytes(&#39;Hi There&#39;), 32));
<a name="line124"></a>}
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/** Regression test for Bug 12863104 */
<a name="line128"></a>function testUpdateWithLongKey() {
<a name="line129"></a>  // Calling update() then digest() should give the same result as just
<a name="line130"></a>  // calling getHmac()
<a name="line131"></a>  var key = hexToBytes(&#39;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#39; +
<a name="line132"></a>                       &#39;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#39; +
<a name="line133"></a>                       &#39;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#39; +
<a name="line134"></a>                       &#39;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#39;);
<a name="line135"></a>  var message = &#39;Secret Message&#39;;
<a name="line136"></a>  var hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), key);
<a name="line137"></a>  hmac.update(message);
<a name="line138"></a>  var result1 = bytesToHex(hmac.digest());
<a name="line139"></a>  hmac.reset();
<a name="line140"></a>  var result2 = bytesToHex(hmac.getHmac(message));
<a name="line141"></a>  assertEquals(&#39;Results must be the same&#39;, result1, result2);
<a name="line142"></a>}
</pre>


</body>
</html>
